<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class ShoppingCart extends Api
{
    protected $check = true;
    public function index_handel($list)
    {
        $SpecificationValue_model = app('app\admin\model\SpecificationValue');
        foreach ($list['data'] as $k=>$v){
        	if(isset($v['specification_sku']['specification_value_ids'])){
        	    if($v['specification_sku']['specification_value_ids'] == '0'){
        	        $list['data'][$k]['gg_name'] = '无规格';
        	    }else{
        	        $gg = explode(',',$v['specification_sku']['specification_value_ids']);
    	            $list['data'][$k]['gg_name'] = '';
    	            foreach ($gg as $vv){
    	                $gg_find = $SpecificationValue_model->where('id',$vv)->find()->toArray();
    	                if(empty($list['data'][$k]['gg_name'])){
    	                    $list['data'][$k]['gg_name'] = $gg_find['name'];
    	                }else{
    	                    $list['data'][$k]['gg_name'] .= ('/'.$gg_find['name']);
    	                }
    	            }
        	    }
        		
        	}
            
        }
        return parent::index_handel($list); // TODO: Change the autogenerated stub
    }
    //列表接口
    public function index()
    {
        if (input('post.limit')) {
            $this->limit = input('post.limit');
        }
        $post = array_filter(input('post.'));
        if(isset($post['where']) && is_array($post['where'])){
            foreach ($post['where'] as $k=>$v){
                if($k == 'name'){
                    $this->where[] = [$k,'like','%'.$v.'%'];
                }elseif($k == 'user_index_id'){
                    if(!isset($this->userinfo) || empty($this->userinfo)){
                        $this->checkLogin();
                    }
                    $this->where[] = [$k,'=',$this->userinfo['id']];
                }else{
                    $this->where[] = [$k,'=',$v];
                }
            }
        }
        $order = $post['order'] ?? [];
        if (!empty($order)) {
            if (is_string($order)) {
                if (strstr($order, ',')) {
                    $order = explode(',', $order);
                    $arr = [];
                    foreach ($order as $k => $v) {
                        $arr[$v] = 'desc';
                    }
                    $order = $arr;
                }
            }
        } elseif(!empty($this->order)){
            $order = $this->order;
        } else {
            $order = 'id desc';
        }
        $this->where[] = ['user_index_id','=',$this->userinfo['id']];
        if ($this->select) {
            $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->select();
        } else {
            if(input('find')){
                $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->find();
            }else{

                $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->paginate($this->limit);
            }
        }
        if (!empty($list)) {
            $list = $list->toArray();
            $list = $this->index_handel($list);
            foreach ($list['data'] as $kk=>$vv){
                if(!empty($vv['product']['images'])&&is_string($vv['product']['images'])){
                    $images = explode(',',$vv['product']['images']);
                    foreach ($images as $kkk=>$vvv)
                    {
                        $images[$kkk] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $vvv;
                    }    
                    $list['data'][$kk]['product']['images'] = $images;
                }else{
                    $list['data'][$kk]['product']['images'] = [];
                }
            }
        }
        return datasuccess($list);
    }
    //数据添加
    public function add()
    {
        $post = array_filter(input('post.'));
        if(!empty($post)){
        	$post['user_index_id'] = $this->userinfo['id'];
        	if(!isset($post['num']) || !isset($post['product_id']) || !isset($post['specification_sku_id'])) return msgerror('参数缺失');
        	if($post['num'] < 1){
        		return msgerror('数量不能小于1');
        	}
        	$sku = app('app\api\model\SpecificationSku')->where([['id','=',$post['specification_sku_id']],['product_id','=',$post['product_id']]])->find();
        	if(empty($sku)){
        		return msgerror('该规格商品不存在');
        	}
        	$data = $this->model->where([['user_index_id','=',$this->userinfo['id']],['specification_sku_id','=',$post['specification_sku_id']]])->find();
        	if(!empty($data) && !empty($data)){
        		$data = $data->toArray();
        		$num = $data['num']*1+$post['num'];
        		$this->model::update(['id'=>$data['id'],'num'=>$num]);
        	}else{
            	$this->model->save($post);
        	}
            return datasuccess();
        }else{
            return msgerror('参数缺失');
        }
    }
    //数据修改
    public function upload()
    {
        $post = array_filter(input('post.'));
        if(!empty($post)){
            if(!isset($post['id'])) return msgerror('缺少id');
            $post = $this->upload_handle($post);
            if(isset($post['status']) && $post['status'] == 'error'){
                return msgerror($post['msg'] ?? '参数缺失');
            }
            unset($post['specification_sku_id']);
            unset($post['product_id']);
            if($post['num'] == 0){
            	return msgerror('数量不能小于1');
            }
            $a = $this->model::update($post);
            return datasuccess();
        }else{
            return msgerror('缺少参数');
        }
    }
}
